﻿using System;
using System.Collections.Generic;
using System.Data;
using Smarly.Bussines.Extentions;
using Smarly.Bussines.Membership;
using Smarly.Core;
using Smarly.Core.Database;

namespace Smarly.Bussines.Model.Payment.YandexMoney
{
	public class PurchaseItem
	{
		public long PurchaseId { get; set; }
		public string PurchaseHashCode { get; set; }
//		public string CharpterHashCode { get; set; }
		public string Price { get; set; }

//		public string ChapterName { get; set; }
//		public string BookName { get; set; }
		public DateTime CreateUtcDate { get; set; }
		public Guid BookId { get; set; }

		public PurchaseItem Get ()
		{
			SqlRepository.ExecuteProcedureReader("Purchase_GetItemInfo",
																					 new List<SpParam>
			                                     	{
			                                     		new SpParam {Name = "@purchaseId", Value = PurchaseId, Type = DbType.Int64},
			                                     	}, null,
																					 reader =>
																					 {
																						 while (reader.Read())
																						 {
																							 PurchaseId = reader.GetInt64("PurchaseId");
//																							 var IsPublishedChapter = reader.GetBoolean("IsPublishedChapter");
																							 Price = reader.GetString("Value");

//																							 BookName = reader.GetString("BookName");
//																							 ChapterName = reader.GetString("ChapterName");
//																							 CharpterHashCode = reader.GetString("CharpterHashCode");
																							 //CreateUtcDate = reader.GetDateTime("UTCCreateDate");
//																							 long? bookTreeId = reader.GetInt64OrNull("BookTreeId");
//																							 BookId = reader.GetGuid("BookId");
																						 }
																						 return this;
																					 });

			return this;
		}

		public PurchaseItem Post(SmarlyIdentity identity)
		{
			SqlRepository.ExecuteProcedureReader("Purchase_CreateItem",
																					 new List<SpParam>
			                                     	{
			                                     		new SpParam {Name = "@userId", Value = identity.UserId, Type = DbType.Guid},
																							new SpParam {Name = "@bookId", Value = BookId, Type = DbType.Guid},
																							new SpParam {Name = "@value", Value = Price, Type = DbType.String},
			                                     	}, null,
																					 reader =>
																					 {
																						 while (reader.Read())
																						 {
																							 PurchaseId = reader.GetInt64("PurchaseId");
																							 PurchaseHashCode = reader.GetString("PurchaseHashCode");
																							 Price = reader.GetString("Value");
																						 }
																						 return this;
																					 });

			return this;
		}


	}
}